package com.repository.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSourceFactory;

public class DataSourceUtil {
	static Properties pros ;
	static String pros_config = "druid_mysql.properties";
	static DataSource ds;
	
	/*加载druid_mysql.properties*/
	static{
		InputStream is = null;
		try {
			pros = new Properties();
			is = Thread.currentThread().getContextClassLoader().getResourceAsStream(pros_config);
			pros.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(is!=null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 获取数据源
	 * @throws Exception 
	 */
	
	public static final DataSource getDataSource() {
		if(ds!=null){
			return ds;
		}
		try {
			ds = DruidDataSourceFactory.createDataSource(pros);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ds;
	}
	
	
}
